<?php

namespace app\admin\controller\admin;

use support\Request;
use app\admin\controller\BaseController;
use app\admin\service\admin\MenuService;

class MenuController extends BaseController
{
    protected array $noNeedLogin = [];

    public function __construct(MenuService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index(Request $request)
    {
        $do = $request->get('do');
        if ($request->isAjax() && $do == 'json') {
            $where = [];

            $result = $this->service->getDataList($where);

            if ($result['count'] == 0) {
                $data = [
                    'code' => -1,
                    'msg' => '未查询到数据'
                ];
            } else {
                $data = [
                    'code' => 0,
                    'msg' => '',
                    'count' => $result['count'],
                    'data' => $result['list']
                ];
            }
            return json($data);
        }

        return $this->fetch('admin/menu/index');
    }

    public function add(Request $request)
    {
        if ($request->isAjax()) {
            $data = [];
            $do = $request->get('do', '');
            if ($do == '') {
                $data = $this->checkField(['pid', 'name', 'url|null', 'act|null', 'link|null', 'icon|null', 'sort|0', 'is_menu|1', 'status|1']);
                return $this->service->setMenu($data);
            }
            if ($do == 'batch') {
                $fileData = $this->checkField(['pid', 'name']);
                $nameText = str_replace(["\r\n", "\r"], "\n", $fileData['name']);
                $nameArr = explode("\n", $nameText);
                foreach ($nameArr as $v) {
                    $data[] = [
                        'pid' => $fileData['pid'],
                        'name' => $v,
                    ];
                }
                return $this->service->setMenuAll($data);
            }
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function edit(Request $request)
    {
        if ($request->isAjax()) {
            $data = [];
            $do = $request->get('do', '');
            if ($do == '') {
                $data = $this->checkField(['id', 'pid', 'name', 'url|null', 'act|null', 'link|null', 'icon|null', 'sort|0', 'is_menu|1', 'status|1']);
            }
            if ($do == 'fast') {
                $fileData = $this->checkField(['id', 'af', 'av|null']);
                $data = [
                    'id' => $fileData['id'],
                    $fileData['af'] => $fileData['av'],
                ];
            }
            if ($do == 'switch') {
                $fileData = $this->checkField(['id', 'af', 'av|1']);
                $data = [
                    'id' => $fileData['id'],
                    $fileData['af'] => $fileData['av'],
                ];
            }

            if (!empty($data)) {
                return $this->service->setMenu($data);
            }
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function del(Request $request)
    {
        if ($request->isAjax()) {
            $data = $this->checkField(['id']);
            return $this->service->delMenu($data);
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

}